<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of soaUsers
 *
 * @author dxx
 */

namespace app\common\model\jsm;
use think\Model;

class Messages extends Model {

    protected $table = 'jj_messages';
    protected $base_table = 'jj_messages';
    protected $connection = ['database' => 'jooj_jsm'];
    
    public function __construct($mp_Id = []) {
        if(!is_array($mp_Id)){
            $this->table = $this->base_table."_".$mp_Id;
        }
        parent::__construct($mp_Id);
    }

    public function getMessagesByCondition($condition,$field='*'){
        return $this->where($condition)->field($field)->find();
    }
    
    public function addMessages($params){
        $data = [
            'openid'=>$params['openid'],
            'nickname'=>isset($params['nickname'])?$params['nickname']:'',
            'headimgurl'=>isset($params['headimgurl'])?$params['headimgurl']:'',
            'staff_id'=>isset($params['staff_id'])?$params['staff_id']:'',
            'msg_id'=>isset($params['msg_id'])?$params['msg_id']:'',
            'is_keyword'=>isset($params['is_keyword'])?$params['is_keyword']:'',
            'msg_type'=>isset($params['msg_type'])?$params['msg_type']:'',
            'content'=>isset($params['content'])?$params['content']:'',
            'create_ts'=> isset($params['create_ts'])?$params['create_ts']:'',
            'status'=>isset($params['status'])?$params['status']:0,
            'return_msg'=>isset($params['return_msg'])?$params['return_msg']:''
        ];
        
        $data = array_filter($data);
        
        return $this->insertGetId($data);
    }
}
